package com.wuliu.controller;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.wuliu.common.annotation.NoAuth;
import com.wuliu.common.constant.Constants;
import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.dto.CustInfoDto;
import com.wuliu.system.domain.vo.BasCustVo;
import com.wuliu.system.service.LoginService;
import com.wuliu.system.service.WeixinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author realue
 * @version 1.0
 * @date 2023-10-05 11:01
 */
@RestController
@RequestMapping("/applet/sys")
@Api(tags = {"登录控制器"})
public class LoginController {
    @Resource
    private WeixinService weixinService;
    @Resource
    private LoginService loginService;

    /**
     * @param code
     * @param sysAccount
     * @return
     */
    @NoAuth
    @ApiOperation(value = "获取JsCode")
    @GetMapping("/getJsCode")
    public ApiResult<WxMaJscode2SessionResult> getJsCode(String code, String sysAccount) {
        try {
            if (StringUtils.isEmpty(sysAccount)) {
                sysAccount = Constants.DEFAULT_SYSACCOUNT;
            }
            return ApiResult.success(weixinService.getJsCode(code, sysAccount));
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
    }

    @NoAuth
    @ApiOperation(value = "授权登录")
    @GetMapping("/authLogin")
    public ApiResult<BasCustVo> authLogin(@Validated CustInfoDto custInfoDto) {
        if (StringUtils.isEmpty(custInfoDto.getSysAccount())) {
            custInfoDto.setSysAccount(Constants.DEFAULT_SYSACCOUNT);
        }
        return ApiResult.success(weixinService.login(custInfoDto));
    }

    @NoAuth
    @ApiOperation(value = "获取绑定手机号", notes = "前端调用wx.getPhoneNumber获取code")
    @GetMapping("/getPhone")
    public ApiResult<String> getPhone(String code, String sysAccount) {
        try {
            if (StringUtils.isEmpty(sysAccount)) {
                sysAccount = Constants.DEFAULT_SYSACCOUNT;
            }
            return ApiResult.success(weixinService.getPhone(code, sysAccount));
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
    }

    @NoAuth
    @ApiOperation(value = "验证码登录", notes = "")
    @GetMapping("/login")
    public ApiResult<BasCustVo> login(String mobile, String code, String sysAccount) {
        if (StringUtils.isEmpty(sysAccount)) {
            sysAccount = Constants.DEFAULT_SYSACCOUNT;
        }
        return ApiResult.success(loginService.login(mobile, code, sysAccount));
    }

    @NoAuth
    @GetMapping("/sendSms")
    @ApiOperation(value = "发送验证码", notes = "")
    public ApiResult<Boolean> sendSms(String mobile, String sysAccount) {
        return ApiResult.success(loginService.sendSms(mobile, sysAccount));
    }

    @NoAuth
    @ApiOperation(value = "密码登录", notes = "")
    @GetMapping("/loginByPwd")
    public ApiResult<BasCustVo> loginByPwd(String loginNo, String password, String sysAccount) {
        if (StringUtils.isEmpty(sysAccount)) {
            sysAccount = Constants.DEFAULT_SYSACCOUNT;
        }
        return ApiResult.success(loginService.loginByPwd(loginNo, password, sysAccount));
    }
}
